package Aleehot100;

/**
 * ClassName: c044
 * Package: Aleehot100
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/7/15 08:41
 * @Version 1.0
 * @Since 1.0
 */
public class c044 {
    public static void main(String[] args) {
        TreeNode node3 = new TreeNode(3);
        TreeNode node1 = new TreeNode(1);
        TreeNode node4 = new TreeNode(4);
        TreeNode node2 = new TreeNode(2);

        node3.left = node1;
        node3.right = node4;
        node1.right = node2;

        System.out.println(kthSmallest(node3, 1));
    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    static int count;
    static int res;

    public static int kthSmallest(TreeNode root, int k) {
        count = 0;
        res = 0;
        process(root, k);
        return res;
    }

    private static void process(TreeNode root, int k) {
        if (count == k) {
            return;
        }
        if (root == null) {
            return;
        }
        process(root.left, k);
        if(++ count == k) {
            res = root.val;
            return;
        }
        process(root.right, k);
    }
}
